package com.touni.fileoperate;

import com.touni.utils.FileOperateUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName : FileCommonOperate
 * @Description : 文件常规操作
 * @Author : lwq
 * @Date: 2020-10-12 16:47
 */
public class FileCommonOperate {

    public StringBuilder contentFromRead = new StringBuilder();

    public String targetEncode = "UTF-8";

    public Set<String> existRuleNameList = new HashSet<>();


    /**
     * 读取某一路径下的所有文件的内容
     * @param filePath 文件的路径
     * @param isIteration 是否迭代获取文件夹里面的文件夹
     * @param fileSuffix 需要获取的后缀名称
     */
    public void loadFile2String(String filePath,Boolean isIteration,String[] fileSuffix){
        File files = new File(filePath);
        File[] listFiles = files.listFiles();
        for (File file : listFiles){
            if (isIteration){
                if (file.isDirectory()){
                    loadFile2String(file.getAbsolutePath(),isIteration,fileSuffix);
                }
            }
            //读取特定后缀的文件内容 写入到contentFromRead中，每行数据以换行符分割
            for (String str : fileSuffix){
                if (file.getName().endsWith(str)){
                    BufferedReader bufferedReader = null;
                    try {
                        String lastTempStr = "";
                        String lastLastTempStr = "";
                        String charSet = FileOperateUtils.getCharSet(file);
                        bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),charSet));
                        String tempStr;
                        int maxXuhao = 0;
                        int prtXuhao = 0;
                        boolean flag = false;
                        String newRuleName = null;
                        while ((tempStr = bufferedReader.readLine()) != null){
                            try {
                                if (!"".equals(tempStr) && tempStr.contains(",")){
                                    int indexOf = tempStr.indexOf(",");
                                    String splitFirst = tempStr.substring(indexOf + 2);
                                    int indexSecond = splitFirst.indexOf(",");
                                    String ruleName = splitFirst.substring(0,indexSecond-1);
                                    String substring = splitFirst.substring(indexSecond + 2);
                                    int xuhao = Integer.valueOf(substring.substring(0, substring.indexOf(",") - 1));
                                    if (null == newRuleName){
                                        newRuleName = ruleName;
                                    }
                                    if (ruleName.equals(newRuleName)){
                                        if (tempStr.contains("RPT_AMOUNT")){
                                            if (lastTempStr.contains("OR") && (!lastTempStr.contains("RIGHT_PARENTHESIS") || !lastTempStr.contains("LEFT_PARENTHESIS"))){
                                                flag = true;
                                            }
                                            prtXuhao = xuhao;
                                        }else {
                                            maxXuhao = xuhao;
                                        }
                                    }
                                    if (!ruleName.equals(newRuleName)){
                                        boolean add = existRuleNameList.add(newRuleName);
                                        if (!add){
                                            System.out.println(newRuleName);

                                        }
                                    /*if (prtXuhao != 0 && prtXuhao < maxXuhao && true){
                                        System.out.println(lastTempStr);
                                    }*/
                                        newRuleName = ruleName;
                                        maxXuhao = xuhao;
                                    }
                                    lastLastTempStr = lastTempStr;
                                    lastTempStr = tempStr;
                                }

                            }catch (Exception e){
                            }

                            //todo 可对内容进行处理
                            /*if (!"UTF-8".equals(charSet)){
                                tempStr = new String(tempStr.getBytes(targetEncode),targetEncode);
                            }
                            if (!lastTempStr.equals(tempStr)){
                                contentFromRead.append(tempStr).append(System.lineSeparator());
                            }
                            if (lastTempStr.contains("RPT_AMOUNT") && !"".equals(tempStr)){
                                System.out.println(lastTempStr);
                            }
                            if (lastTempStr.contains("RPT_AMOUNT") && (!lastTempStr.contains("LEFT_PARENTHESIS") && !lastTempStr.contains("RIGHT_PARENTHESIS")) && tempStr.contains("OR")){
                                System.out.println(lastTempStr);
                            }
                            lastTempStr = tempStr;*/
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        if (bufferedReader != null){
                            try {
                                bufferedReader.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                contentFromRead.append(System.lineSeparator());
            }
        }
    }




    public void writeContent2File(String content,String path){
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(path));
            bufferedWriter.write(content);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedWriter.flush();
                bufferedWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }



}
